package com.baizhi.controller;

import com.baizhi.domain.dto.ArticleEditDto;
import com.baizhi.domain.dto.ArticlePageDto;
import com.baizhi.domain.dto.ArticleSaveDto;
import com.baizhi.domain.dto.ArticleSearchDto;
import com.baizhi.domain.es.EsArticle;
import com.baizhi.domain.jpa.Article;
import com.baizhi.domain.vo.ArticleDetailVo;
import com.baizhi.domain.vo.PageVo;
import com.baizhi.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("article")
public class ArticleController {
    private static final Logger log = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    // 查询文章列表
    @GetMapping
    public ResponseEntity<PageVo<List<Article>>> findPage(@RequestBody ArticlePageDto param) {
        log.info("article page ...");
        log.info("param : {}", param);
        PageVo<List<Article>> page = articleService.findPage(param);
        return ResponseEntity.ok(page);
    }

    // 查看文章详情
    @GetMapping("{id}")
    public ResponseEntity<ArticleDetailVo> findById(@PathVariable("id") Integer id) {
        log.info("article detail ...");
        log.info("id : {}", id);
        ArticleDetailVo detail = articleService.findById(id);
        return ResponseEntity.ok(detail);
    }

    // 保存文章
    @PostMapping("save")
    public ResponseEntity<Boolean> saveArticle(@Validated @RequestBody ArticleSaveDto param) {
        log.info("article save ...");
        log.info("param : {}", param);
        articleService.saveArticle(param);
        return ResponseEntity.ok(true);
    }

    // 更新文章
    @PostMapping("edit")
    public ResponseEntity<Boolean> editArticle(@Validated @RequestBody ArticleEditDto param) {
        log.info("article edit ...");
        log.info("param : {}", param);
        articleService.updateArticle(param);
        return ResponseEntity.ok(true);
    }

    // 删除文章
    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteArticle(@PathVariable("id") Integer id) {
        log.info("article delete ...");
        log.info("id : {}", id);
        articleService.deleteByAid(id);
        return ResponseEntity.ok(true);
    }

    // 文章检索
    @GetMapping("search")
    public ResponseEntity<PageVo<List<EsArticle>>> searchArticle(@RequestBody ArticleSearchDto param) {
        log.info("article search ...");
        log.info("param : {}", param);
        PageVo<List<EsArticle>> page = articleService.searchArticle(param);
        return ResponseEntity.ok(page);
    }
}
